package day041;

import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class 客户端 {

    ObjectInputStream ois;  //准备一个东西的发送管道
    ObjectOutputStream oos; //
    Scanner sc = new Scanner(System.in);
    public void start() throws UnknownHostException, IOException {
        Socket s = new Socket("192.168.215.110",4444);
        OutputStream out = s.getOutputStream();
        InputStream in = s.getInputStream();
        ois  = new ObjectInputStream(in);
        oos = new ObjectOutputStream(out);
        new 接收().start();
        new 发送().start();
    }
//    SuperRobot sr = new SuperRobot();
    class 接收 extends Thread {
        public void run() {
            for (;;) {
                try {
                    User u = (User)ois.readObject();
                    System.out.println(u.intName+"说"+u.isay);
//                    sr.speech(100,10,u.intName+"对您说"+ u.isay);
                } catch (IOException | ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    class 发送 extends Thread {
        public void run() {
            for (;;) {
                System.out.println("请您输入：");
                String isay = sc.next();
                User u = new User();
                u.isay = isay;
                try {
                    oos.writeObject(u);
                    oos.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) throws IOException {
        new 客户端().start();
    }
}
